package com.ruiyian.project.tunnel.service.impl;

import com.ruiyian.common.annotation.DataScope;
import com.ruiyian.project.tunnel.domain.Hole;
import com.ruiyian.project.tunnel.domain.Tunnel;
import com.ruiyian.project.tunnel.mapper.HoleMapper;
import com.ruiyian.project.tunnel.mapper.TunnelMapper;
import com.ruiyian.project.tunnel.service.TunnelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chong
 * @email 673426164@qq.com
 * @create 2021/1/20
 * Desc: 隧道信息实现
 */
@Service
public class TunnelServiceImpl implements TunnelService {
    @Autowired
    private TunnelMapper tunnelMapper;
    @Autowired
    private HoleMapper holeMapper;

    /**
     * 列表
     * @param tunnel
     * @return
     */
    @DataScope(deptAlias = "d")
    @Override
    public List<Tunnel> selectTunnelList(Tunnel tunnel) {
        return tunnelMapper.selectTunnelList(tunnel);
    }

    /**
     * 删除
     * @param id
     */
    @Transactional
    @Override
    public void delete(Long id) {
        tunnelMapper.delete(id);
        holeMapper.deleteByTunId(id);
    }

    /**
     * 保存
     * @param tunnel
     */
    @Override
    public void save(Tunnel tunnel) {
        tunnelMapper.save(tunnel);
    }

    /**
     * 保存隧道洞信息
     * @param tunnel
     */
    @Transactional
    @Override
    public void saveAll(Tunnel tunnel) {
        this.save(tunnel);
        if(null!= tunnel && tunnel.getHoleList().size()>0){
            tunnel.getHoleList().stream().peek(p->{p.setTunnelId(tunnel.getId());}).collect(Collectors.toList());
            holeMapper.batchSave(tunnel.getHoleList());
        }
    }
    /**
     * 根据id获取对象
     * @param id
     * @return
     */
    @Override
    public Tunnel selectTunnelById(Long id) {
        return tunnelMapper.selectTunnelById(id);
    }

    /**
     * 更新
     * @param tunnel
     */
    @Transactional
    @Override
    public void update(Tunnel tunnel) {
        tunnelMapper.update(tunnel);
        if(tunnel.getHoleList()!=null && tunnel.getHoleList().size()>0){
            for(Hole hole: tunnel.getHoleList()){
                hole.setTunnelId(tunnel.getId());
                if(hole.getHoleId()!=null){
                    holeMapper.updateHole(hole);
                }else {
                    holeMapper.save(hole);
                }
            }
        }
//        holeMapper.updateBatch(tunnel.getHoleList());
    }
    /**
     * 根据洞id删除
     * @param id
     */
    @Override
    public void deleteHole(Long id) {
        holeMapper.delete(id);
    }

    /**
     * 批量删除
     * @param list
     */
    @Transactional
    @Override
    public void deleteBatch(List<Tunnel> list) {
        tunnelMapper.deleteBatch(list);
        List<Hole> holes = new ArrayList<>();
        list.stream().peek(p->{
            Hole hole = new Hole();
            hole.setTunnelId(p.getId());
            holes.add(hole);
        }).collect(Collectors.toList());
        holeMapper.updateBatch(holes);
    }


    @Override
    public List<Hole> selectHoleById(Long tunnelId) {
        return holeMapper.selectHoleById(tunnelId);
    }

    @Override
    public void updateTitle(Tunnel tunnel) {
        tunnelMapper.updateTitle(tunnel);
    }
}
